package com.qfedu.labsystem.controller;

import com.qfedu.labsystem.constant.SYSConstant;
import com.qfedu.labsystem.entity.vo.ResultOBJ;
import com.qfedu.labsystem.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @ClassName UploadController
 * @Author ykx
 * @Date 2024/4/8 16:59
 * @Version 1.0
 */
@RestController
@RequestMapping("/adminapi/upload")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @PostMapping
    public ResultOBJ upload(@RequestParam("file") MultipartFile file) {
        try {
            uploadService.upload(file);
            return new ResultOBJ(SYSConstant.CODE_SUCCESS, "上传成功");
        } catch (Exception e) {
            return new ResultOBJ(SYSConstant.CODE_SUCCESS, "上传失败");
        }
    }

    @PostMapping("/import")
    public ResultOBJ importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        return ResultOBJ.success(uploadService.importExcel(file));
    }

    @PostMapping("/csv")
    public ResultOBJ importCsv(@RequestParam("file") MultipartFile file) throws IOException {
        return ResultOBJ.success(uploadService.importCsv(file));
    }

    @PostMapping("/image")
    public ResultOBJ uploadImage(@RequestParam("file") MultipartFile file, @RequestParam("id") Integer id) throws Exception {
        return ResultOBJ.success(uploadService.uploadImage(file, id));
    }

}
